package com.example.administrator.x3picturetransliteration;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
 * 用于组建之间消息传递(key根据需求传递就好了，要确保唯一性，用完要记得注销以免造成内存泄漏)
 * Created by Administrator on 2019/4/15.
 */

public class RxBus {

    private static RxBus rxBus;

    private CompositeSubscription mCompositeSubscriptions; //管理Rx订阅关系

    private Observable<Object> mObservable; //被观察者
    private Subscriber<? super Object> subscriber;  //用来发送数据

    private Map<Object, Observer<Object>> mObserver; //一堆的观察者

    private RxBus() {

    }

    public static RxBus getInstance() {
        if (rxBus == null) {
            synchronized (RxBus.class) {
                if (rxBus == null) {
                    rxBus = new RxBus();
                }
            }
        }
        return rxBus;
    }

    /**
     * 启动RxBus实例化各种乱七八糟的东西并创建一个订阅关系
     */
    public void start() {

        mCompositeSubscriptions = new CompositeSubscription();

        mObserver = new HashMap<>();

        mObservable = Observable.create(new Observable.OnSubscribe<Object>() {

            @Override
            public void call(Subscriber<? super Object> subscriber) {

                RxBus.this.subscriber = subscriber;
            }
        });


        mCompositeSubscriptions.add(

                mObservable.subscribe(new Observer<Object>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {


                    }

                    @Override
                    public void onNext(Object o) {

                        Map<Object, Object> map = (Map<Object, Object>) o;

                        for (Object key : map.keySet()) {

                            if (mObserver.containsKey(key)) {

                                mObserver.get(key).onNext(map.get(key));
                            }
                        }
                    }
                })
        );

    }

    /**
     * 干掉订阅关系
     */
    public void end() {

        if (mCompositeSubscriptions == null) {

            Log.e("RxBus", "请在Application中开启RxBus");
            return;
        }

        mCompositeSubscriptions.unsubscribe();
    }

    /**
     * 向某个组件发送数据
     */
    public void post(Object key, Object data) {

        if (subscriber == null) {

            Log.e("RxBus", "请在Application中开启RxBus");
            return;
        }

        Map<Object, Object> map = new HashMap<>();
        map.put(key, data);

        subscriber.onNext(map);
    }

    /**
     * 在用到的组件注册
     */
    public void Register(Object key, Observer<Object> data) {

        if (subscriber == null) {

            Log.e("RxBus", "请在Application中开启RxBus");
            return;
        }

        if (!mObserver.containsKey(key)) {

            mObserver.put(key, data);
        }

    }

    /**
     * 在用到的组件注销
     */
    public void Cancellation(Object key) {

        if (subscriber == null) {

            Log.e("RxBus", "请在Application中开启RxBus");
            return;
        }

        if (mObserver.containsKey(key)) mObserver.remove(key);

    }

}
